package com.team.app.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.team.app.model.Company;
import com.team.app.model.Employee;

@Repository
public class CompanyDaoImpl extends AbstractDao<Company> implements CompanyDao {
	CompanyDaoImpl() {
		super(Company.class);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Company> findAll(Company obj) {
		// TODO Auto-generated method stub
		String hql = "from Company o where 1=1 ";
		if(obj.getCompanyName() !=null){
			hql += " and companyName like '%"+obj.getCompanyName().trim()+"%'";
		}
		if(obj.getCompanyEmail() !=null ){
			hql += " and companyEmail like '%"+obj.getCompanyEmail().trim()+"%'";
		}
		Session  s = getCurrentSession();
		Query q = s.createQuery(hql);
		
		List<Company> result = q.list();
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public boolean existEmail(String email) {
		// TODO Auto-generated method stub
		Session s=this.getCurrentSession();		
			Query q = s.createQuery("from Company o where  o.companyEmail = ?");
			q.setParameter(0, email);		
			List<Employee> result = q.list();	
			if(result.size() > 0){
				return true;
			}			
			return false;		
	}

//	public void createCompanyInfo(Company comp, Employee emp,Auth auth) {
//		Session  session = getCurrentSession();
//		
//		session.persist(comp);
//		session.flush();   
//		
//		emp.setCompanyId(comp); 
//	    
//		session.persist(emp);
//		session.flush();   
//		
//		auth.setCompanyId(comp.getCompanyId());
//		session.persist(auth);		
//		session.flush();   
//	}
}
